/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package threadserver;

import java.io.IOException;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Bryce
 */
public class Client implements Runnable{
    
    private Socket socket;
    private byte[] buffer;
    private ClientLogger logger;
    
    public Client(Socket clientSocket, ClientLogger logger) {
        this.socket = clientSocket;
        buffer = new byte[ThreadServer.BUFFER_SIZE];
        this.logger = logger;
    }

    @Override
    public void run() {
        //Read data from the client until closed.
        while(true) {
            try {
                int r = socket.getInputStream().read(buffer);
                if(r < 0) {
                    // Client closed connection, so exit.
                    ThreadServer.concurrentClients--;
                    socket.close();
                    return;
                }
                // Send data back to client.
                socket.getOutputStream().write(buffer);
            } catch (IOException ex) {
                try {
                    ThreadServer.concurrentClients--;
                    this.socket.close();
                    return;
                } catch (IOException ex1) {
                    Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex1);
                }
                
            }
        }
        
    }
}
